<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            库间转移
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">来源仓库</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="选择/扫描 来源仓库" id="workCenterCode" value="" onclick="getIdSelector();" />
                    </div>
                    <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectWorkCenter('1')"></i>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">调出库位</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="选择/扫描 调出库位" id="workCellCode" value="" onclick="getIdSelector();" />
                    </div>
                    <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectWorkCell('1')"></i>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">目标仓库</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="选择/扫描 目标仓库" id="relWorkCenterCode" value="" onclick="getIdSelector();" />
                    </div>
                    <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectWorkCenter('2')"></i>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">调入库位</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="选择/扫描 调入库位" id="relWorkCellCode" value="" onclick="getIdSelector();" />
                    </div>
                    <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectWorkCell('2')"></i>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">物料号</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请 录入/扫描物料号" id="mrlCode" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item" style="display:none">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">批次号</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="lotCode" value="000000">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">转移数量</div>
                    <div class="aui-list-item-input">
                        <input type="number" id="qty">
                    </div>
                </div>
            </li>
        </ul>
        <p></p>
        <p></p>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="save()">保存</div>
        <div style="background-color:white; height: 270px;width: 100%;margin-top:3%;border:solid 1px #D3D3D3;display:none" id="tableDiv">
            <table class="table" style='width:100%; font-size:14px;' id="table">
                <tbody>
                    <tr>
                        <td style="width:20%;text-align:right;">来源仓库:</td>
                        <td id="workCenterNameT"></td>
                    </tr>
                    <tr>
                        <td style="width:20%;text-align:right;">调出库位:</td>
                        <td id="workCellNameT"></td>
                    </tr>
                    <tr>
                        <td style="width:20%;text-align:right;">目标仓库:</td>
                        <td id="relWorkCenterNameT"></td>
                    </tr>
                    <tr>
                        <td style="width:20%;text-align:right;">调入库位:</td>
                        <td id="relWorkCellNameT"></td>
                    </tr>
                    <tr>
                        <td style="width:20%;text-align:right;">物&nbsp&nbsp料&nbsp&nbsp号:</td>
                        <td id="mrlCodeT"></td>
                    </tr>
                    <!-- <tr>
                        <td style="width:20%;text-align:right;">批&nbsp&nbsp次&nbsp&nbsp号:</td>
                        <td id="lotCodeT"></td>
                    </tr> -->
                    <tr>
                        <td style="width:20%;text-align:right;">转移数量:</td>
                        <td id="qtyT"></td>
                    </tr>
                    <tr>
                        <td style="width:20%;text-align:right;">操&nbsp&nbsp作&nbsp&nbsp者:</td>
                        <td id="createIdT"></td>
                    </tr>
                    <tr>
                        <td style="width:20%;text-align:right;">操作时间:</td>
                        <td id="createDateT"></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";
    var workCenterGid = ""; //来源仓库GID
    var workCenterCode = ""; //来源仓库编码
    var workCenterName = ""; //来源仓库名称
    var workCellGid = ""; //调出库位GID
    var workCellCode = ""; //调出库位编码
    var workCellName = ""; //调出库位名称

    var relWorkCenterGid = ""; //目标仓库GID
    var relWorkCenterCode = ""; //目标仓库编码
    var relWorkCenterName = ""; //目标仓库名称
    var relWorkCellGid = ""; //调入库位GID
    var relWorkCellCode = ""; //调入库位编码
    var relWorkCellName = ""; //调入库位名称

    var workCenterSelectFlag = ""; //仓库选择事件
    var workCellSelectFlag = ""; //库位选择事件

    //初始化查询仓库下拉框
    apiready = function() {

        $("#workCenterCode").focus();
        idSelector = "workCenterCode";
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function(ret, err) {

            //1：来源仓库
            if (idSelector == 'workCenterCode') {

                $("#workCenterCode").val(ret);
                //查询仓库
                getWorkCenter(ret, idSelector, "1");

                //2:调出库位
            } else if (idSelector == 'workCellCode') {

                $("#workCellCode").val(ret);
                //查询调出库位
                getWorkCell(ret, idSelector, "1");

                //3：目标仓库
            } else if (idSelector == 'relWorkCenterCode') {

                $("#relWorkCenterCode").val(ret);
                //查询仓库
                getWorkCenter(ret, idSelector, "2");

                //4：调入库位
            } else if (idSelector == 'relWorkCellCode') {

                $("#relWorkCellCode").val(ret);
                //查询调入库位
                getWorkCell(ret, idSelector, "2");

                //5：扫描物料号
            } else if (idSelector == 'mrlCode') {

                $("#mrlCode").val(ret);
            }
        });
    }

    //1：仓库扫描校验
    function getWorkCenter(code, inputId, workCenterFlag) {
        var serverUrl = localStorage.getItem('serverUrl');
        var un = localStorage.getItem('un');
        var url = serverUrl + "/app/getWorkCenter";
        var data = {
            values: {
                workCenterCode: code,
                mrlCode: "",
                lotCode: "",
                qty: "",
                un:un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    var list = eval(myobj.list);
                    if (list.length > 0) {

                        //源仓库
                        if (workCenterFlag == "1") {
                            workCenterGid = list[0].workCenterGid;
                            workCenterCode = list[0].workCenterCode + ":" + list[0].workCenterName;
                            workCenterName = list[0].workCenterName;
                            document.getElementById(inputId).value = workCenterCode;

                            //目标仓库
                        } else {
                            relWorkCenterGid = list[0].workCenterGid;
                            relWorkCenterCode = list[0].workCenterCode + ":" + list[0].workCenterName;
                            relWorkCenterName = list[0].workCenterName;
                            document.getElementById(inputId).value = relWorkCenterCode;
                        }

                    } else {

                        //源仓库
                        if (workCenterFlag == "1") {
                            workCenterGid = "";
                            workCenterCode = "";
                            workCenterName = "";
                            document.getElementById(inputId).value = workCenterCode;

                            //目标仓库
                        } else {
                            relWorkCenterGid = "";
                            relWorkCenterCode = "";
                            relWorkCenterName = "";
                            document.getElementById(inputId).value = relWorkCenterCode;
                        }

                        api.toast({
                            msg: "仓库编码【" + code + "】不存在!",
                            duration: 3000
                        });
                    }
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //2：选中仓库
    function goSelectWorkCenter(workCenterFlag) {
        workCenterSelectFlag = workCenterFlag;
        api.openWin({
            name: 'uwmSelectWorkCenter',
            url: 'uwmSelectWorkCenter.html',
            pageParam: {
                flag: "in",
                name: "uwmInterBankTransferTjcl",
                mrlCode: "",
                lotCode: "",
                qty: ""
            }
        });
    }

    //3:仓库选择后回调函数
    function setWorkCenter(id, code, name) {
        if (workCenterSelectFlag == "1") {
            workCenterGid = id;
            workCenterCode = code;
            workCenterName = name;
            document.getElementById('workCenterCode').value = code + ":" + name;
        } else if (workCenterSelectFlag == "2") {
            relWorkCenterGid = id;
            relWorkCenterCode = code;
            relWorkCenterName = name;
            document.getElementById('relWorkCenterCode').value = code + ":" + name;
        }
    }

    //4:来源仓库PDA自带扫描回车事件
    $("#workCenterCode").on('keypress', function(e) {
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {

            //仓库编码
            $("#workCenterCode").val(sm);
            //校验仓库编码是否存在
            getWorkCenter(sm, "workCenterCode", "1");
        }
    });

    //5:目标仓库PDA自带扫描回车事件
    $("#relWorkCenterCode").on('keypress', function(e) {
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {

            //仓库编码
            $("#relWorkCenterCode").val(sm);
            //校验仓库编码是否存在
            getWorkCenter(sm, "relWorkCenterCode", "2");
        }
    });

    //7:库位扫描校验
    function getWorkCell(code, inputId, flag) {

        var id = "";
        //源库位扫描前先扫来源仓库
        if (inputId == "workCellCode") {
            var workCenterCodeR = $("#workCenterCode").val();
            if (workCenterGid == "" || workCenterGid == null || workCenterGid == undefined) {
                api.toast({
                    msg: "请先选择/扫描 来源仓库",
                    duration: 3000
                });
                $("#workCellCode").val("");
                workCellCode = "";
                workCellGid = "";
                workCellName = "";
                return;
            }

            if (workCenterCodeR == "" || workCenterCodeR == null || workCenterCodeR == undefined) {
                api.toast({
                    msg: "请先选择/扫描 来源仓库",
                    duration: 3000
                });
                $("#workCellCode").val("");
                workCellCode = "";
                workCellGid = "";
                workCellName = "";
                return;
            }

            id = workCenterGid;

            //目标
        } else {
            var relWorkCenterCodeR = $("#relWorkCenterCode").val();
            if (relWorkCenterGid == "" || relWorkCenterGid == null || relWorkCenterGid == undefined) {
                api.toast({
                    msg: "请先选择/扫描 目标仓库",
                    duration: 3000
                });
                $("#workCellCode").val("");
                workCellCode = "";
                workCellGid = "";
                workCellName = "";
                return;
            }

            if (relWorkCenterCodeR == "" || relWorkCenterCodeR == null || relWorkCenterCodeR == undefined) {
                api.toast({
                    msg: "请先选择/扫描 目标仓库",
                    duration: 3000
                });
                $("#relWorkCellCode").val("");
                relWorkCellCode = "";
                relWorkCellGid = "";
                relWorkCellName = "";
                return;
            }
            id = relWorkCenterGid;
        }

        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getWorkCell";
        var data = {
            values: {
                flag: "in_rk", //库内库间标识（out_kn库内  out_kj库间,in_rk入库出库）
                workCenterGid: id, //仓库GID
                workCellCode: code, //库位编码
                relWorkCellGid: "", //调入库位
                tm: "",
                workCellFlag: "", //原库位调入库位flag
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {
                    var list = eval(myobj.list);

                    //源库位
                    if (flag == "1") {

                        if (list.length > 0) {
                            workCellGid = list[0].workCellGid;
                            workCellCode = list[0].workCellCode + ":" + list[0].workCellName;
                            workCellName = list[0].workCellName;
                            document.getElementById(inputId).value = workCellCode;
                        } else {
                            workCellGid = "";
                            workCellCode = "";
                            workCellName = "";
                            document.getElementById(inputId).value = workCellCode;

                            api.toast({
                                msg: "库位编码【" + code + "】不存在!",
                                duration: 3000
                            });
                        }

                        //调入库位
                    } else if (flag == "2") {
                        if (list.length > 0) {
                            relWorkCellGid = list[0].workCellGid;
                            relWorkCellCode = list[0].workCellCode + ":" + list[0].workCellName;
                            relWorkCellName = list[0].workCellName;
                            document.getElementById(inputId).value = relWorkCellCode;
                        } else {
                            relWorkCellGid = "";
                            relWorkCellCode = "";
                            relWorkCellName = "";
                            document.getElementById(inputId).value = relWorkCellCode;

                            api.toast({
                                msg: "库位编码【" + code + "】不存在!",
                                duration: 3000
                            });
                        }
                    }
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //8：选择库位
    function goSelectWorkCell(workCellFlag) {
        workCellSelectFlag = workCellFlag;
        var id = "";

        //来源仓库
        if (workCellFlag == "1") {
            var workCenterCodeR = $("#workCenterCode").val();
            //来源仓库非空校验
            if (workCenterGid == "" || workCenterGid == null || workCenterGid == undefined) {
                api.toast({
                    msg: "请先选择/扫描 来源仓库",
                    duration: 3000
                });
                return;
            }

            //来源仓库非空校验
            if (workCenterCodeR == "" || workCenterCodeR == null || workCenterCodeR == undefined) {
                api.toast({
                    msg: "请先选择/扫描 来源仓库",
                    duration: 3000
                });
                return;
            }

            id = workCenterGid;

            //目标仓库
        } else {
            var relWorkCenterCodeR = $("#relWorkCenterCode").val();
            //目标仓库非空校验
            if (relWorkCenterGid == "" || relWorkCenterGid == null || relWorkCenterGid == undefined) {
                api.toast({
                    msg: "请先选择/扫描 仓库",
                    duration: 3000
                });
                return;
            }

            //目标仓库非空校验
            if (relWorkCenterCodeR == "" || relWorkCenterCodeR == null || relWorkCenterCodeR == undefined) {
                api.toast({
                    msg: "请先选择/扫描 目标仓库",
                    duration: 3000
                });
                return;
            }
            id = relWorkCenterGid;
        }

        api.openWin({
            name: 'uwmSelectWorkCell',
            url: 'uwmSelectWorkCell.html',
            pageParam: {
                flag: "in_rk",
                name: "uwmInterBankTransferTjcl",
                tm: "",
                workCenterGid: id,
                relWorkCellGid: "",
                workCellFlag: ""
            }
        });
    }

    //库位选择后回调函数
    function setWorkCell(id, code, name) {

        if (workCellSelectFlag == "1") {

            workCellGid = id;
            workCellCode = code;
            workCellName = name;
            document.getElementById('workCellCode').value = code + ":" + name;

        } else {
            relWorkCellGid = id;
            relWorkCellCode = code;
            relWorkCellName = name;
            document.getElementById('relWorkCellCode').value = code + ":" + name;
        }
    }

    //9；PDA自带扫描回车事件-源库位
    $("#workCellCode").on('keypress', function(e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {

            $("#workCellCode").val(sm);
            getWorkCell(sm, "workCellCode", "1");
        }
    });

    //10；PDA自带扫描回车事件-调入库位
    $("#relWorkCellCode").on('keypress', function(e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {

            $("#relWorkCellCode").val(sm);
            getWorkCell(sm, "relWorkCellCode", "2");
        }
    });

    //保存按钮
    function save() {

        //物料号
        var mrlCode = $("#mrlCode").val();
        //批次号
        var lotCode = $("#lotCode").val();
        //转移数量数量
        var qty = $("#qty").val();
        //源仓库编码
        var workCenterCodeR = $("#workCenterCode").val();
        //源库位编码
        var workCellCodeR = $("#workCellCode").val();
        //目标仓库编码
        var relWorkCenterCodeR = $("#relWorkCenterCode").val();
        //调入库位编码
        var relWorkCellCodeR = $("#relWorkCellCode").val();

        //源仓库非空校验
        if (workCenterCodeR == "" || workCenterCodeR == null || workCenterCodeR == undefined) {
            api.toast({
                msg: '请选择/扫描 源仓库',
                duration: 3000
            });
            return;
        }

        //源仓库非空校验
        if (workCenterGid == "" || workCenterGid == null || workCenterGid == undefined) {
            api.toast({
                msg: '请选择/扫描 源仓库',
                duration: 3000
            });
            return;
        }

        //源库位非空校验
        if (workCellCodeR == "" || workCellCodeR == null || workCellCodeR == undefined) {
            api.toast({
                msg: '请选择/扫描 源库位',
                duration: 3000
            });
            return;
        }

        //源库位非空校验
        if (workCellGid == "" || workCellGid == null || workCellGid == undefined) {
            api.toast({
                msg: '请选择/扫描 源库位',
                duration: 3000
            });
            return;
        }

        //目标仓库非空校验
        if (relWorkCenterCodeR == "" || relWorkCenterCodeR == null || relWorkCenterCodeR == undefined) {
            api.toast({
                msg: '请选择/扫描 目标仓库',
                duration: 3000
            });
            return;
        }

        //目标仓库非空校验
        if (relWorkCenterGid == "" || relWorkCenterGid == null || relWorkCenterGid == undefined) {
            api.toast({
                msg: '请选择/扫描 目标仓库',
                duration: 3000
            });
            return;
        }

        //调入库位非空校验
        if (relWorkCellCodeR == "" || relWorkCellCodeR == null || relWorkCellCodeR == undefined) {
            api.toast({
                msg: '请选择/扫描 调入库位',
                duration: 3000
            });
            return;
        }

        //调入库位非空校验
        if (relWorkCellGid == "" || relWorkCellGid == null || relWorkCellGid == undefined) {
            api.toast({
                msg: '请选择/扫描 调入库位',
                duration: 3000
            });
            return;
        }

        //物料非空校验
        if (mrlCode == "" || mrlCode == null || mrlCode == undefined) {
            api.toast({
                msg: '请 录入/扫描物料号',
                duration: 3000
            });
            return;
        }

        //批次号非空校验
        if (lotCode == "" || lotCode == null || lotCode == undefined) {
            api.toast({
                msg: '批次号不能为空',
                duration: 3000
            });
            return;
        }

        //转移数量非空校验
        if (qty == "" || qty == null || qty == undefined) {
            api.toast({
                msg: '请输入转移数量',
                duration: 3000
            });
            return;
        } else {
            if (parseFloat(qty) <= 0) {
                api.toast({
                    msg: '转移数量必须大于0',
                    duration: 3000
                });
                return;
            }
        }

        //源仓库和目标仓库不能一样
        if (workCenterGid == relWorkCenterGid) {
            api.toast({
                msg: '源仓库和目标仓库不能一样',
                duration: 3000
            });
            return;
        }

        //源仓库和目标仓库不能一样
        if (workCenterCodeR == relWorkCenterCodeR) {
            api.toast({
                msg: '源仓库和目标仓库不能一样',
                duration: 3000
            });
            return;
        }

        //源库位和调入库位不能一样
        if (workCellCodeR == relWorkCellCodeR) {
            api.toast({
                msg: '源库位和调入库位不能一样',
                duration: 3000
            });
            return;
        }

        //源库位和调入库位不能一样
        if (workCellGid == relWorkCellGid) {
            api.toast({
                msg: '源库位和调入库位不能一样',
                duration: 3000
            });
            return;
        }

        api.showProgress({
            title: '保存中...',
        });
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/uwmInterBankTransferSaveTjclCommon";
        var data = {
            values: {
                workCenterGid: workCenterGid, //来源仓库
                workCellGid: workCellGid, //源库位编码
                relWorkCenterGid: relWorkCenterGid, //目标仓库
                relWorkCellGid: relWorkCellGid, // 调入库位
                mrlCode: mrlCode, //物料号
                lotCode: lotCode, //批次号
                qty: qty, //转移数量
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {
                    api.hideProgress();
                    api.toast({
                        msg: "保存成功",
                        duration: 3000
                    }); //清空表单

                    $("#mrlCode").val("");
                    $("#qty").val("");

                    var map = myobj.data;
                    for (var key in map) {
                        $("#" + key).html(map[key]);
                    }
                    $("#tableDiv").css('display', 'block');
                } else if (myobj.code == '1') {
                    api.hideProgress();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                api.hideProgress();
            }
        });
    }

    //物料号监听回车事件
    $("#mrlCode").on('keypress', function(e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {

            $("#mrlCode").val();
        }
    });

    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>
